<?php
/**
 * 
 * Validation class
 * 
 * @category Abovo
 * 
 * @package Abovo_Valid
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 */

/**
 * 
 * Validation class
 * 
 * Adds some methods to Solar_Valid
 * 
 * @category Abovo
 * 
 * @package Abovo_Valid
 * 
 */
class Abovo_Valid extends Solar_Valid {
    
    /**
     * 
     * Validates if a country code is a valid country code
     * 
     * This is case-sensitive. I.e a valid code: 'FI'.
     * 
     * @param string $value Country code
     * 
     * @param int $blank Allow blank value
     * 
     * @return bool
     * 
     */
    public function country($value, $blank = Solar_Valid::NOT_BLANK)
    {
        if ($blank && $this->blank($value)) {
            return true;
        }
        
        $countrys = Solar::factory('Lux_Intl')->getCountryList();
        return in_array($value, array_keys($countrys));
    }
    
    /**
     * 
     * Validates that a value is valid VAT number
     * 
     * @param string $value VAT number. i.e 'FI12345678'
     * 
     * @param int $blank Allow blank value
     * 
     * @return bool
     * 
     */
    public function vat($value, $blank = Solar_Valid::NOT_BLANK)
    {
        if ($blank && $this->blank($value)) {
            return true;
        }
        
        if (strlen($value) === 10) {
            // country code
            $country = substr($value, 0, 2);
            
            // take the end off and cast to int
            $end = substr($value, 2, 8);
            
            // match country as a valid country code and
            // the rest of the value as digits
            if($this->country($country) && $this->ctype($end, 'digit')) {
                return true;
            } 
        } 
        
        // failed
        return false;
    }
    
    /**
     * 
     * Validates that a value is a valid ZIP code
     * 
     * @param string|int $value ZIP code
     * 
     * @param int $blank Allow blank value or not
     * 
     * @return bool
     * 
     */
    public function zip($value, $blank = Solar_Valid::NOT_BLANK)
    {
        if ($blank && $this->blank($value)) {
            return true;
        }
        
        if (strlen($value) === 5 && $this->integer($value)) {
            return true;
        }
        
        return false;
    }
    
    /**
     * 
     * Validates that a value is phone number
     * 
     * Value needs to have a leading '+' sign, three numbers
     * followed by 4 to 10 number.
     * 
     * @return bool
     * 
     */
    public function tel($value, $blank = Solar_Valid::NOT_BLANK)
    {
        if ($blank && $this->blank($value)) {
            return true;
        }
        
        return $this->regex($value, "/\+[0-9]{2,3}[0-9]{4,10}$/");
    }
    
    /**
     * 
     * Validates that a string has no whitespace chars
     * 
     * @return void
     * 
     */
    public function wikiPage($value, $blank = Solar_Valid::NOT_BLANK)
    {
        if ($blank && $this->blank($value)) {
            return true;
        }
        
        return $this->regex($value, "/^[^\s]+$/D");
    }
}
